﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Prism.Regions;
using Castle.Windsor;
using Tanmia.Basis.ServiceInterface;
using Tanmia.Basis.Core;
using Tanmia.Basis.ViewModel;
using Castle.MicroKernel.Registration;
using Tanmia.Basis.Security;
using Microsoft.Practices.ServiceLocation;

namespace Castle.Windsor
{
    public static class WindsorContainerExtensions
    {
        public static void RegisterView<TView, TViewModel, TBusinessObject>(this IWindsorContainer container, NavigationItem parent, string regionName)
            where TBusinessObject : BaseBusinessObject, new()
            where TViewModel : BaseEditAndBrowseViewModel<TBusinessObject>
        {
            IAuthorizationService authorizationService = ServiceLocator.Current.GetInstance<IAuthorizationService>();
            INavigationService navigationService = ServiceLocator.Current.GetInstance<INavigationService>();

            if (authorizationService.IsAuthorize(string.Format("/{0}/{1}", parent.ViewName, typeof(TView).Name)))
            {
                if (!container.Kernel.HasComponent(typeof(TViewModel)))
                    container.Register(Component.For<TViewModel>().LifeStyle.Singleton);

                TViewModel viewModel = container.Resolve<TViewModel>();

                viewModel.ViewInfo.RegionName = regionName;
                viewModel.ViewInfo.ViewName = typeof(TView).Name;

                container.Register(Component.For<TView>().Named(typeof(TView).Name).LifeStyle.Singleton);

                navigationService.RegisterNavigatorItem(new NavigationItem
                {
                    Title = viewModel.ViewInfo.Title,
                    Description = viewModel.ViewInfo.Description,
                    RegionName = regionName,
                    ViewName = typeof(TView).Name,
                    Parent = parent
                });
            }
        }

        public static void RegisterBrowserView<TView, TViewModel, TBusinessObject>(this IWindsorContainer container, NavigationItem parent, string regionName)
            where TBusinessObject : BaseBusinessObject, new()
            where TViewModel : BaseEditAndBrowseViewModel<TBusinessObject>
        {
            IAuthorizationService authorizationService = ServiceLocator.Current.GetInstance<IAuthorizationService>();
            INavigationService navigationService = ServiceLocator.Current.GetInstance<INavigationService>();

            if (authorizationService.IsAuthorize(string.Format("/{0}/{1}", parent.ViewName, typeof(TView).Name)))
            {
                if (!container.Kernel.HasComponent(typeof(TViewModel)))
                    container.Register(Component.For<TViewModel>().LifeStyle.Singleton);

                TViewModel viewModel = container.Resolve<TViewModel>();

                viewModel.BrowserInfo.RegionName = regionName;
                viewModel.BrowserInfo.ViewName = typeof(TView).Name;

                container.Register(Component.For<TView>().Named(typeof(TView).Name).LifeStyle.Singleton);

                navigationService.RegisterNavigatorItem(new NavigationItem
                {
                    Title = viewModel.BrowserInfo.Title,
                    Description = viewModel.BrowserInfo.Description,
                    RegionName = regionName,
                    ViewName = typeof(TView).Name,
                    Parent = parent
                });
            }
        }
    }
}
